<?php
$section = 'product_detail';
$titre = _LB_PRODUCT_MANAGEMENT;
$smarty->assign("titre", $titre);

//get object ID
$id = Request::getIntNumber('id');
$id_product = Request::getIntNumber('id_product');
$act = Request::getIntNumber('act');

// set backlink is product/list
$backlink = Session::getBackLink("product", Utils::getHomeLink());

$product_info = new Product_Info();	
if ($id_product) {
	$product_info->ID_PRODUCT = $id_product;
	$product = new Product();
	if (!$product->get($id_product)) {
		redirect($backlink);
	} else {
		// set backlink is product/viewDetail
		$backlink = Session::getBackLink($section, $backlink);
	}
}

if ($id) {
	if (!$product_info->get($id) || ($id_product && $id_product != $product_info->ID_PRODUCT)) {
		redirect($backlink);
	} else {
		$backlink = Session::getBackLink($section, $backlink);
	}
}

//if action = UPDATE or CREATE
if ($act == ACT_VALID_EDIT)
{
	$product_info->NAME = trim(Request::get('NAME'));
	$product_info->CONTENT = trim($_POST['CONTENT']);
	$product_info->DISABLED = Request::getNumber('DISABLED');

	if ($product_info->validateInput())
	{
		$product_info->save();

		redirect($backlink);
	}
}

include_once CKEDITOR_BASE_DIR. "ckeditor.php";
$CKEditor = new CKEditor();
$CKEditor->basePath = CKEDITOR_BASE_PATH;
$CKEditor->config['height'] = 600;
$CKEditor->config['filebrowserBrowseUrl'] = CKFINDER_BASE_PATH.'ckfinder.html';
$CKEditor->config['filebrowserImageBrowseUrl'] = CKFINDER_BASE_PATH.'ckfinder.html?type=Images';
$CKEditor->config['filebrowserFlashBrowseUrl'] = CKFINDER_BASE_PATH.'ckfinder.html?type=Flash';
$CKEditor->config['filebrowserUploadUrl'] = CKFINDER_BASE_PATH.'core/connector/php/connector.php?command=QuickUpload&type=Files';
$CKEditor->config['filebrowserImageUploadUrl'] = CKFINDER_BASE_PATH.'core/connector/php/connector.php?command=QuickUpload&type=Images';
$CKEditor->config['filebrowserFlashUploadUrl'] = CKFINDER_BASE_PATH.'core/connector/php/connector.php?command=QuickUpload&type=Flash';
//create cfer
$cfer = new Cfer(array(
	_LB_ACCUEIL => Utils::getHomeLink(),
	_LB_PRODUCT_MANAGEMENT => Session::getBackLink("product"),
	_LB_PRODUCT_DETAIL => Session::getBackLink($section),
	($id == 0? _LB_ADD_PRODUCT_INFO : _LB_EDIT_PRODUCT_INFO) => Utils::getCurrentLink() ));

//if validate is false, display form again
$smarty->assign('cfer', $cfer);
$smarty->assign('product_info', $product_info);
$smarty->assign('backlink', $backlink);
$smarty->assign('CKEditor', $CKEditor);
$smarty->display('product_info_form.html');